package top.superflow.flow.runtime.flow

import top.superflow.exception.Exceptable
import top.superflow.flow.runtime.Executable
import top.superflow.flow.runtime.route.ExtRoutedNode

import scala.beans.BeanProperty


class ExtFlow extends Executable[FlowRequest, FlowResponse] with Exceptable{


  @BeanProperty
  var start : String = _

  @BeanProperty
  var routes : Map[String, ExtRoutedNode] = Map[String, ExtRoutedNode]()

  private val session = new FlowSession

  
  
  def execute(request : FlowRequest, response : FlowResponse) : Unit = {
    val flowRequest = initFlowRequest(request)

    if(response == null){
      throwRequestUnsupportedException("The response could not be null!")
    }

    executeFromStart(flowRequest, response)
  }

  private def executeFromStart(request : FlowRequest, response : FlowResponse) : Unit = {
    val startRoute = routes.get(start)
    if(startRoute == None){
      throwRequestUnsupportedException("The start route {} could not be null", start)
    }

    startRoute.get.execute(request, response)
  }




  private def initFlowRequest(request : FlowRequest) : FlowRequest = {
    val flowRequest = if(request == null) new FlowRequest else request

    flowRequest.setSession(session)
    
    flowRequest
  }

}